require '~coffee/_lib/spin/init.coffee'

$._ajax = $.ajax

$.ajax = ->
    Spin.inc()
    $._ajax.apply(@,arguments).always ->
        Spin.done()

$.postJSON = (url, data, success)->
    if typeof(data) != 'string'
        data = JSON.stringify(data)
        contentType = "application/json"
    else
        contentType = "text/plain"
    $.ajax(
        url
        {
            type: 'POST'
            contentType
            data
            processData: false
            success
        }
    )

$(document).ajaxError (event, {status}, {url}) ->
    url = $.escape url
    switch status
        when 200,412 then return
        when 403 then GO.ln("auth/login")
        when 0 then $.toast("""<a target="_blank" href="#{url}">网络请求失败</a>""").addClass('ERR')
        else
            $.box.err(
                """<p>请求网址 :</p><p style="word-break:break-all"><a target="_blank" href="#{url}">#{url}</p>"""
                title:"错误代码: #{status}"
            )

